#include "stm32f10x.h"                  // Device header
#include "MyRTC.h"
#include "OLED.h"
#include "Key.h"
#include "LED.h"
#include "SetTime.h"
#include "StopClock.h"
#include "FlashLight.h"
#include "Balance.h"
#include "game_dino.h"
#include "Face.h"

uint8_t KeyNum;
void Peripheral_Init(void)					//外设初始化
{
	MyRTC_Init();
	MyRTC_SetTime();
	Key_Init();
	LED_Init();

	
}
/****************************首页时钟*********************************/

void Show_Main_Clock(void)				//首页时钟
{
	MyRTC_ReadTime();
	OLED_Printf(0,0,OLED_6X8,"%d-%d-%d",MyRTC_Time[0],MyRTC_Time[1],MyRTC_Time[2]);
	OLED_Printf(16,16,OLED_12X24,"%02d:%02d:%02d",MyRTC_Time[3],MyRTC_Time[4],MyRTC_Time[5]);
	OLED_ShowString(0,48,"菜单",OLED_8X16);
	OLED_ShowString(96,48,"设置",OLED_8X16);
	
}

int clkflag=1;

int Show_Clock_First(void)				//首页显示
{
	while(1)
	{
		KeyNum=Key_GetNum();
		if(KeyNum==1)		//上一项
		{	
			clkflag--;
			if(clkflag<=0)clkflag=2;
		}else if(KeyNum==2)	//下一项
		{	
			clkflag++;
			if(clkflag>=3)clkflag=1;
			
		}else if(KeyNum==3)	//确定
		{	
			OLED_Clear();
			OLED_Update();
			return clkflag;
		}
		
		switch(clkflag)
		{
			case 1:
				Show_Main_Clock();
				OLED_ReverseArea(0,48,32,16);
				OLED_Update();
				break;
			case 2:
				Show_Main_Clock();
				OLED_ReverseArea(96,48,32,16);
				OLED_Update();
				break;
		}
	
	}
}
/******************************设置界面*******************************/
void Show_Setting_UI(void)
{
	OLED_Clear();
	OLED_ShowImage(0,0,16,16,Image_Return);
	OLED_ShowString(0,16,"时间设置",OLED_8X16);
}

uint8_t setflag=1;//设置页面标志位
uint8_t Show_Setting_Detail(void)
{
	while(1)
	{
		KeyNum=Key_GetNum();
		uint8_t setflag_temp=0;
		
		
		if(KeyNum==1)		//上一项
		{	
			setflag--;
			if(setflag<=0)setflag=2;
		}else if(KeyNum==2)	//下一项
		{	
			setflag++;
			if(setflag>=3)setflag=1;
			
		}else if(KeyNum==3)	//确定
		{	
			OLED_Clear();
			OLED_Update();
			setflag_temp=setflag;
		}
		
		if(setflag_temp==1){return 0;}
		else if(setflag_temp==2){Show_Setting_Time();}
		
		switch(setflag)
		{
			case 1:
				Show_Setting_UI();
				OLED_ReverseArea(0,0,16,16);
				OLED_Update();
				break;
			case 2:
				Show_Setting_UI();
				OLED_ReverseArea(0,16,64,16);
				OLED_Update();
				break;
		}
	
	}
}

/****************************菜单滑动界面*********************************/


uint8_t now_select;//当前选项
uint8_t target_select;//目标选项
uint8_t x_now=48;//上次选项的x坐标
uint8_t Speed=4;//速度
uint8_t move_flag;//1:开始移动，0：停止移动


void Menu_Animation(void)
{
	OLED_Clear();
	OLED_ShowImage(42,10,44,44,Image_Change);
	
	if(now_select<target_select)
	{	
		x_now-=Speed;
		if(x_now==0)
		{
			now_select++;
			move_flag=0;
			x_now=48;
		}
	}
	if(now_select>target_select)
	{	
		x_now+=Speed;
		if(x_now==96)
		{
			now_select--;
			move_flag=0;
			x_now=48;
		}
	}
	if(now_select>=1)//当前选项为1时，即是第二个图案
	{
		OLED_ShowImage(x_now-48,16,32,32,Menu_Graph[now_select-1]);
	}
	if(now_select>=2)
	{
		OLED_ShowImage(x_now-96,16,32,32,Menu_Graph[now_select-2]);
	}
	OLED_ShowImage(x_now,16,32,32,Menu_Graph[now_select]);
	OLED_ShowImage(x_now+48,16,32,32,Menu_Graph[now_select+1]);
	OLED_ShowImage(x_now+96,16,32,32,Menu_Graph[now_select+2]);
	
	OLED_Update();
}



void Set_Select(uint8_t move_flag,uint8_t Now_Select,uint8_t Target_Select)
{
	if(move_flag==1)
	{
		now_select=Now_Select;
		target_select=Target_Select;
	
	}
	Menu_Animation();
}


uint8_t menu_flag=1;
uint8_t Show_Menu_UI(void)
{
	move_flag=1;
	uint8_t DirectFlag=2;//移动到上下项 标志位
	while(1)
	{
		KeyNum=Key_GetNum();
		uint8_t menu_flag_temp=0;
		
		
		if(KeyNum==1)		//上一项
		{	
			DirectFlag=1;
			move_flag=1;
			menu_flag--;
			if(menu_flag<=0)menu_flag=7;
		}else if(KeyNum==2)	//下一项
		{	
			DirectFlag=2;
			move_flag=1;
			menu_flag++;
			if(menu_flag>=8)menu_flag=1;
			
		}else if(KeyNum==3)	//确定
		{	
			OLED_Clear();
			OLED_Update();
			menu_flag_temp=menu_flag;
		}
		
		if(menu_flag_temp==1){return 0;}
		else if(menu_flag_temp==2){StopClock();}
		else if(menu_flag_temp==3){FlashLight();}
		else if(menu_flag_temp==4){Face();}
		else if(menu_flag_temp==5){Balance();}
		else if(menu_flag_temp==6){Gradienter();}
		else if(menu_flag_temp==7){Game();}
		
			//这部分是根据menu_flag_temp显示
		if(menu_flag==1)
		{
			if(DirectFlag==1)Set_Select(move_flag,1,0);
			else if(DirectFlag==2)Set_Select(move_flag,0,0);
		}
		else
		{
			if(DirectFlag==1)Set_Select(move_flag,menu_flag,menu_flag-1);
			else if(DirectFlag==2)Set_Select(move_flag,menu_flag-2,menu_flag-1);
		}
		
		//此代码与上文有相同效果
		/*
		
		switch(menu_flag)
		{
			
			case 1:
				if(DirectFlag==1)Set_Select(move_flag,1,0);
				else if(DirectFlag==2)Set_Select(move_flag,0,0);
				break;
			case 2:
				if(DirectFlag==1)Set_Select(move_flag,1,0);
				else if(DirectFlag==2)Set_Select(move_flag,0,1);
				break;
			case 3:
				
				break;
			case 4:
				
				break;
			case 5:
				
				break;
			case 6:
				
				break;
			case 7:
				
				break;
		}
	*/

	}
}

